<?php
/**
 * 原型类
 */

class Prototype extends MyBase{
	
	public static $prototypes = null;
	
	function __construct() {
		parent::__construct('prototype.id', array('keyword' => 'name'));
	}
	
	/**
	 * 修改原型
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		$select = array('name' => $post['name']);
		if($id > 0){
			$select['kept'] = " AND id!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)) {
			return $this->rwarn(lang('already') . lang('exist'));
		}
		$data = array(
			'type'		=> $post['type'],
			'name'		=> $post['name'],
			'value'		=> $this->format_value_in($post['type'], $post['value']),
			'prompt'	=> $post['prompt'],
			'regexp'	=> $post['regexp'],
			'rank'		=> $post['rank'],
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			return $this->cache_prototype();
		}
		return $r;
	}
			
	/**
	 * 删除原型
	 */
	public function del($post){
		if(!is_array($post['list']))$post['list'] = array($post['list']);
		$iRtn = 0;
		foreach($post['list'] as $id){
			if($this->db->delete($this->tbname, array('id' => $id))){
				$iRtn ++;
			}
		}
		if($iRtn > 0){
			return $this->cache_prototype();
		}
		return $iRtn;
	}
	
	// 更新排序
	public function rank($post){
		if(!is_array($post['rank']))return false;
		$iRtn = 0;
		foreach($post['rank'] as $id => $rank){
			$data = array(
				'rank'		 => $rank, 
				'updatetime' => TIME,
			);
			if($this->db->update($this->tbname, array('id' => $id), $data)){
				$iRtn ++;
			}
		}
		if($iRtn > 0){
			return $this->cache_prototype();
		}else{
			return $iRtn;
		}
	}

	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
	
	public function cache_prototype(){
		$prototype_list = $this->db->get_all($this->tbname, '*');
		$prototypes = array('id_name_list' => null);
		$prototype_id_name_arr = array();
		foreach($prototype_list['list'] as $var_info){
			if($var_info['type'] == '1'){
				$var_info['value'] = unserialize($var_info['value']);
			}elseif($var_info['type'] == '2'){
				$var_info['value'] = intval($var_info['value']);
			}else{
				$var_info['value'] = trim($var_info['value']);
			}
			$prototypes[$var_info['id']] = $var_info;
			$prototype_id_name_arr[$var_info['id']] = $var_info['name'];
		}
		$prototypes['id_name_list'] = $prototype_id_name_arr;
		return cache_write('prototypes.php', $prototypes);
	}
	
	public function format_value_in($type, $value){
		if(empty($value)){
			return '';
		}
		if($type == 1){
			$arr = array();
			$str = preg_replace('/(\n|；)/i', ';', $value);
			$str = preg_replace('/(=|＝|：)/i', ':', $str);
			$_arr = explode(';', $str);
			foreach($_arr as $item){
				$_tmp = explode(':', $item);
				$_tmp[0] = trim($_tmp[0]);
				if(empty($_tmp[0]))continue;
				if(isset($_tmp[1])){
					$arr[$_tmp[0]] = trim($_tmp[1]);
				}else{
					$arr[sizeof($arr) + 1] = $_tmp[0];
				}
			}
			return serialize($arr);
		}elseif($type == 2){
			return intval($value);
		}elseif($type == 3){
			return trim($value);
		}
		return null;
	}
	
	public function format_value_out($type, $value){
		if($type == 1){
			$_arr = @unserialize($value);
			$arr = array();
			foreach($_arr as $key => $val){
				$arr[] = "$key:$val";
			}
			return implode("\n", $arr);
		}elseif($type == 2){
			return intval($value);
		}elseif($type == 3){
			return trim($value);
		}
		return null;
	}
	
	// 读取缓存
	public static function get_prototypes($name = ''){
		if(empty(self::$prototypes)){
			self::$prototypes = cache_read('prototypes.php');
		}
		if($name != '' && isset(self::$prototypes[$name])){
			return self::$prototypes[$name];
		}
		return self::$prototypes;
	}
}
?>